route.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { getServerSideConfig } from "@/app/config/server";
  2. import {
  3. TENCENT_BASE_URL,
  4. ApiPath,
  5. ModelProvider,
  6. ServiceProvider,
  7. Tencent,
  8. } from "@/app/constant";
  9. import { prettyObject } from "@/app/utils/format";
  10. import { NextRequest, NextResponse } from "next/server";
  11. import { auth } from "@/app/api/auth";
  12. import { isModelAvailableInServer } from "@/app/utils/model";
  13. import { getHeader } from "@/app/utils/tencent";
  14. const serverConfig = getServerSideConfig();
  15. async function handle(
  16. req: NextRequest,
  17. { params }: { params: { path: string[] } },
  18. ) {
  19. console.log("[Tencent Route] params ", params);
  20. if (req.method === "OPTIONS") {
  21. return NextResponse.json({ body: "OK" }, { status: 200 });
  22. }
  23. const authResult = auth(req, ModelProvider.Hunyuan);
  24. if (authResult.error) {
  25. return NextResponse.json(authResult, {
  26. status: 401,
  27. });
  28. }
  29. try {
  30. const response = await request(req);
  31. return response;
  32. } catch (e) {
  33. console.error("[Tencent] ", e);
  34. return NextResponse.json(prettyObject(e));
  35. }
  36. }
  37. export const GET = handle;
  38. export const POST = handle;
  39. export const runtime = "nodejs";
  40. export const preferredRegion = [
  41. "arn1",
  42. "bom1",
  43. "cdg1",
  44. "cle1",
  45. "cpt1",
  46. "dub1",
  47. "fra1",
  48. "gru1",
  49. "hnd1",
  50. "iad1",
  51. "icn1",
  52. "kix1",
  53. "lhr1",
  54. "pdx1",
  55. "sfo1",
  56. "sin1",
  57. "syd1",
  58. ];
  59. async function request(req: NextRequest) {
  60. const controller = new AbortController();
  61. // tencent just use base url or just remove the path
  62. let path = `${req.nextUrl.pathname}`.replaceAll(
  63. ApiPath.Tencent + "/" + Tencent.ChatPath,
  64. "",
  65. );
  66. let baseUrl = serverConfig.tencentUrl || TENCENT_BASE_URL;
  67. if (!baseUrl.startsWith("http")) {
  68. baseUrl = `https://${baseUrl}`;
  69. }
  70. if (baseUrl.endsWith("/")) {
  71. baseUrl = baseUrl.slice(0, -1);
  72. }
  73. console.log("[Proxy] ", path);
  74. console.log("[Base Url]", baseUrl);
  75. const timeoutId = setTimeout(
  76. () => {
  77. controller.abort();
  78. },
  79. 10 * 60 * 1000,
  80. );
  81. const fetchUrl = `${baseUrl}${path}`;
  82. const body = await req.text();
  83. const headers = await getHeader(
  84. body,
  85. serverConfig.tencentSecretId as string,
  86. serverConfig.tencentSecretKey as string,
  87. );
  88. const fetchOptions: RequestInit = {
  89. headers,
  90. method: req.method,
  91. body,
  92. redirect: "manual",
  93. // @ts-ignore
  94. duplex: "half",
  95. signal: controller.signal,
  96. };
  97. try {
  98. const res = await fetch(fetchUrl, fetchOptions);
  99. // to prevent browser prompt for credentials
  100. const newHeaders = new Headers(res.headers);
  101. newHeaders.delete("www-authenticate");
  102. // to disable nginx buffering
  103. newHeaders.set("X-Accel-Buffering", "no");
  104. return new Response(res.body, {
  105. status: res.status,
  106. statusText: res.statusText,
  107. headers: newHeaders,
  108. });
  109. } finally {
  110. clearTimeout(timeoutId);
  111. }
  112. }